<template>
    <div>
      <el-row :gutter="6">
        <el-col :span="12">
          <el-card>
            <el-form ref="info" :model="info" label-width="80px">
              <el-form-item label="学号">
                <el-input v-model="info.code" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="姓名">
                <el-input v-model="info.name" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="主讲课程">
                <el-input v-model="info.main" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="辅讲课程">
                <el-input v-model="info.minor" :disabled="true"></el-input>
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
        <el-col :span="12">
          <el-card>
            <el-form ref="info" :model="pwd" label-width="80px">
              <el-form-item label="旧密码">
                <el-input v-model="pwd.lpassword"></el-input>
              </el-form-item>
              <el-form-item label="新密码">
                <el-input type="password" v-model="pwd.password"></el-input>
              </el-form-item>
              <el-form-item label="确认密码">
                <el-input type="password" v-model="pwd.rPassword"></el-input>
              </el-form-item>
            </el-form>
            <el-button @click="updatePwd" type="primary" style="margin-left: 45%">修改密码</el-button>
          </el-card>
        </el-col>
      </el-row>
    </div>
</template>

<script>

  import {mapGetters} from 'vuex'
  import {getInfoById, updatePwd, validPwd} from "../api/teacher";
  import {mixin} from "../mixins";

  export default {
    name: "Teacher",
    mixins: [mixin],
    computed:{
      ...mapGetters([
        'teacherId'
      ])
    },
    data(){
      return{
        info: {
          code: '',
          name: '',
          main: '',
          minor: ''
        },
        pwd: {
          lpassword: '',
          password: '',
          rPassword: ''
        }
      }
    },
    created() {
      this.init();
    },
    methods: {
      init(){
        getInfoById(this.teacherId).then(({data}) => {
          if(data.code === 0){
            this.info.code = data.teacher.code
            this.info.name = data.teacher.name
            this.info.main = data.teacher.main
            this.info.minor = data.teacher.minor
          }else{
            this.notify("初始化错误，请稍后再试", "error")
          }
        }).catch(err => {
          console.error(err)
          this.notify("初始化错误，请稍后再试", "error")
        })
      },
      updatePwd() {
        if(this.pwd.password === this.pwd.rPassword){
          validPwd(this.teacherId, this.pwd.lpassword).then(({data}) => {
            if(data.code === 0){
              updatePwd(this.teacherId, this.pwd.password).then(({data}) => {
                if(data.code === 0){
                  this.notify("修改成功", "success")
                  this.pwd.password = ''
                  this.pwd.lpassword = ''
                  this.pwd.rPassword = ''
                }else{
                  this.notify("修改失败，请稍后再试", "error")
                }
              }).catch(err => {
                console.error(err)
                this.notify("操作失败，请稍后再试", "error")
              })
            }else{
              this.notify("旧密码错误", "info")
            }
          }).catch(err => {
            console.error(err)
            this.notify("操作失败，请稍后再试", "error")
          })
        }else{
          this.notify("两次密码不相同", "info")
        }
      }
    }
  }
</script>

<style scoped>

</style>
